﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Flexibles.CApCableBasicRequiredPointCloudParametersControl
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.Flexibles;

public class CApCableBasicRequiredPointCloudParametersControl : UserControl, IComponentConnector
{
  private const double ForceParameterEpsilon = 0.01;
  private const double ParameterEpsilon = 0.0001;
  private bool m_isControlLoading;
  private readonly List<IDisposable> m_disposableObjects;
  internal TextBlock labelCableName;
  internal CApCableNameTextControl NameEditBoxControl;
  internal TextBlock labelCableStartFrame;
  internal CApCableEditBoxControl StartFrameEditBoxControl;
  internal Button editStartAttachmentFrame;
  internal TextBlock labelCableEndFrame;
  internal CApCableEditBoxControl EndFrameEditBoxControl;
  internal Button editEndAttachmentFrame;
  internal TextBlock labelPointCloud;
  internal CApCableEditBoxControl OptimizationPointCloudLayerEditBoxControl;
  private bool _contentLoaded;

  public CApCableBasicRequiredPointCloudParametersControl()
  {
    this.m_isControlLoading = true;
    this.m_disposableObjects = new List<IDisposable>();
  }

  public CApFLEXCableNewAttributeViewModel AttributeViewModel { get; private set; }

  public ITxCable Cable { get; set; }

  public bool IsEditMode => this.AttributeViewModel.IsEditMode;

  private CUiFLEXAttachmentFrameWindow AttachmentFrameWindow { get; set; }

  private CUiFLEXCreationMode CreationMode => this.AttributeViewModel.CreationMode;

  public void InitControl(
    CApFLEXCableNewAttributeViewModel attributeViewModel,
    ICApCableFocusableControl focusEscapeControl)
  {
    this.AttributeViewModel = attributeViewModel ?? throw new ArgumentNullException(nameof (attributeViewModel));
    this.InitializeComponent();
    IEnumerable<CApFLEXFrameValidator> frameValidators = this.InitializeControlsAndGetFrameValidators();
    this.SetUpDisposableObjects(focusEscapeControl);
    if (!this.IsEditMode)
      return;
    foreach (CApFLEXFrameValidator flexFrameValidator in frameValidators)
      flexFrameValidator.ShowError = false;
    this.StartFrameEditBoxControl.DisplayableObject = this.AttributeViewModel.StartFrame as ITxDisplayableObject;
    this.EndFrameEditBoxControl.DisplayableObject = this.AttributeViewModel.EndFrame as ITxDisplayableObject;
    this.OptimizationPointCloudLayerEditBoxControl.DisplayableObject = this.AttributeViewModel.OptimizationPointCloudLayer as ITxDisplayableObject;
    foreach (CApFLEXFrameValidator flexFrameValidator in frameValidators)
      flexFrameValidator.ShowError = true;
  }

  private IEnumerable<CApFLEXFrameValidator> InitializeControlsAndGetFrameValidators()
  {
    this.NameEditBoxControl.InitializeControl((ICApCableNameViewModel) this.AttributeViewModel);
    CApFLEXFrameSelection selectionFrame1 = new CApFLEXFrameSelection(FrameSelectionType.StartFrame, (ICApCableFrameStatus) this.AttributeViewModel);
    CApFLEXFrameSelection selectionFrame2 = new CApFLEXFrameSelection(FrameSelectionType.EndFrame, (ICApCableFrameStatus) this.AttributeViewModel);
    CApFLEXFrameValidator objectValidator1 = new CApFLEXFrameValidator((ICApFLEXFrameSelection) selectionFrame1, this.AttributeViewModel.CableScopeStatus);
    CApFLEXFrameValidator objectValidator2 = new CApFLEXFrameValidator((ICApFLEXFrameSelection) selectionFrame2, this.AttributeViewModel.CableScopeStatus);
    this.StartFrameEditBoxControl.InitializeControl((ITxValidator) objectValidator1);
    this.EndFrameEditBoxControl.InitializeControl((ITxValidator) objectValidator2);
    this.OptimizationPointCloudLayerEditBoxControl.InitializeControl((ITxValidator) new CApFLEXPointCloudLayerValidator());
    return (IEnumerable<CApFLEXFrameValidator>) new List<CApFLEXFrameValidator>(2)
    {
      objectValidator1,
      objectValidator2
    };
  }

  private void SetUpDisposableObjects(ICApCableFocusableControl focusEscapeControl)
  {
    IEnumerable<CApCableEditBoxControl> cableEditBoxControls = (IEnumerable<CApCableEditBoxControl>) new List<CApCableEditBoxControl>(3)
    {
      this.StartFrameEditBoxControl,
      this.EndFrameEditBoxControl,
      this.OptimizationPointCloudLayerEditBoxControl
    };
    this.m_disposableObjects.Add((IDisposable) this.NameEditBoxControl);
    this.m_disposableObjects.AddRange((IEnumerable<IDisposable>) cableEditBoxControls);
    this.m_disposableObjects.Add((IDisposable) new CApCableStartFrameControlBinder((ICApCableEditBoxControl) this.StartFrameEditBoxControl, (ICApFLEXCableAttributeViewModel) this.AttributeViewModel));
    this.m_disposableObjects.Add((IDisposable) new CApCableEndFrameControlBinder((ICApCableEditBoxControl) this.EndFrameEditBoxControl, (ICApFLEXCableAttributeViewModel) this.AttributeViewModel));
    this.m_disposableObjects.Add((IDisposable) new CApCableOptimizationPointCloudLayerControlBinder((ICApCableEditBoxControl) this.OptimizationPointCloudLayerEditBoxControl, this.AttributeViewModel));
    this.m_disposableObjects.Add((IDisposable) new CApCableControlFocusSwitch(CUiFLEXCreationMode.FROM_POINT_CLOUD, (ICApFLEXCableAttributeViewModel) this.AttributeViewModel, (IEnumerable<ICApCableEditBoxControl>) cableEditBoxControls, focusEscapeControl));
  }

  private void UpdateFrameEditorAfterCableModify()
  {
    this.StartFrameEditBoxControl.DisplayableObject = (ITxDisplayableObject) (this.Cable.StartFrame as TxFrame);
    this.EndFrameEditBoxControl.DisplayableObject = (ITxDisplayableObject) (this.Cable.EndFrame as TxFrame);
    this.AttributeViewModel.StartFrame = (ITxLocatableObject) (this.Cable.StartFrame as TxFrame);
    this.AttributeViewModel.EndFrame = (ITxLocatableObject) (this.Cable.EndFrame as TxFrame);
  }

  public void StartOfLoading() => this.m_isControlLoading = true;

  public void EndOfLoading()
  {
    this.m_isControlLoading = false;
    this.NameEditBoxControl.EndOfLoading();
    if (this.IsEditMode || this.CreationMode != CUiFLEXCreationMode.FROM_POINT_CLOUD)
      return;
    this.StartFrameEditBoxControl.SetFocus();
  }

  private void LaunchAttachmentFrame(FrameSelectionType frameType)
  {
    this.AttachmentFrameWindow = new CUiFLEXAttachmentFrameWindow((ICApFLEXCableAttributeViewModel) this.AttributeViewModel, frameType);
    ((Window) this.AttachmentFrameWindow).Closing += new CancelEventHandler(this.AttachmentWindow_Closing);
    ((Window) this.AttachmentFrameWindow).WindowStartupLocation = WindowStartupLocation.CenterOwner;
    ((Window) this.AttachmentFrameWindow).Show();
    this.IsEnabled = false;
  }

  public void DidDialogClose()
  {
    foreach (IDisposable disposableObject in this.m_disposableObjects)
      disposableObject.Dispose();
    this.m_disposableObjects.Clear();
  }

  private void EditStartAttachmentFrame_Click(object sender, RoutedEventArgs e)
  {
    this.LaunchAttachmentFrame(FrameSelectionType.StartFrame);
  }

  private void EditEndAttachmentFrame_Click(object sender, RoutedEventArgs e)
  {
    this.LaunchAttachmentFrame(FrameSelectionType.EndFrame);
  }

  public void AttachmentWindow_Closing(object sender, CancelEventArgs e)
  {
    ((Window) this.AttachmentFrameWindow).Closing -= new CancelEventHandler(this.AttachmentWindow_Closing);
    this.AttachmentFrameWindow = (CUiFLEXAttachmentFrameWindow) null;
    this.IsEnabled = true;
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/flexibles/usercontrols/capcablebasicrequiredpointcloudparameterscontrol.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  internal Delegate _CreateDelegate(Type delegateType, string handler)
  {
    return Delegate.CreateDelegate(delegateType, (object) this, handler);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        this.labelCableName = (TextBlock) target;
        break;
      case 2:
        this.NameEditBoxControl = (CApCableNameTextControl) target;
        break;
      case 3:
        this.labelCableStartFrame = (TextBlock) target;
        break;
      case 4:
        this.StartFrameEditBoxControl = (CApCableEditBoxControl) target;
        break;
      case 5:
        this.editStartAttachmentFrame = (Button) target;
        this.editStartAttachmentFrame.Click += new RoutedEventHandler(this.EditStartAttachmentFrame_Click);
        break;
      case 6:
        this.labelCableEndFrame = (TextBlock) target;
        break;
      case 7:
        this.EndFrameEditBoxControl = (CApCableEditBoxControl) target;
        break;
      case 8:
        this.editEndAttachmentFrame = (Button) target;
        this.editEndAttachmentFrame.Click += new RoutedEventHandler(this.EditEndAttachmentFrame_Click);
        break;
      case 9:
        this.labelPointCloud = (TextBlock) target;
        break;
      case 10:
        this.OptimizationPointCloudLayerEditBoxControl = (CApCableEditBoxControl) target;
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
